<?php

namespace app\index\dao\traits;

use ShidianUtil\Utils\TimeUtil;
use think\Model;

trait UidTrait
{

    /**
     * @var Model
     */
    private $baseModel;

    /**
     * 生成UID
     * @param $pad_length
     * @return string
     */
    private function getUid($pad_length = 3): string
    {
        $time = time();

        $createdStart = TimeUtil::stamp(TimeUtil::DAY, $time);
        $createdEnd = TimeUtil::stamp(TimeUtil::DAY, $createdStart, false);

        $count = $this->baseModel::timeRange($createdStart, $createdEnd, 'created', 'created')->count();
        $count++;

        if (strlen($count) < $pad_length) {
            $count = str_pad($count, $pad_length, '0', STR_PAD_LEFT);
        }

        $z = date('z', $time);
        if (strlen($z) < 3) {
            $z = str_pad($z, 3, '0', STR_PAD_LEFT);
        }

        return date('y', $time) . $z . $count;
    }
}